function fnSport(addTime, userId, friendId, token) { //初始化
	sportDate(addTime, userId, friendId, token);
}
//接口
function sportDate(addTime, userId, friendId, token) { //运动
	var url = "http://api.sungohealth.com/sport/data";
	var data = {
		addTime: addTime,
		userId: userId,
		friendId: friendId,
		token: token
	};
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: "json",
		success: function(data, status, xhr) {
			console.log(data);
			if(data.status == '0' || data.msg == '成功') {
				$('.unEmpty').removeClass('dn');
				$('.empty').addClass('dn');
				fn_option_sport(addTime, data.data); //数据渲染
			} else {
				$('.unEmpty').addClass('dn');
				$('.empty').removeClass('dn');
			}
		},
		error: function(result) {
			console.log(result);
		}
	});
}

//接口测试数据
//var dataTemp1 = {
//	"status": 0,
//	"msg": "成功",
//	"data": {
//		"sportId": 1,
//		"userId": 1,
//		"dataDate": "2018-03-23",
//		// "sportData": "[\"14\",\"34\",\"36\",\"143\",\"62\",\"47\",\"22\",\"82\",\"920\",\"999\",\"648\",\"34\",\"26\",\"476\",\"496\",\"25\"]",
//		"sportData": '[{"08:51:20":"349"},{"09:01:21":"125"},{"09:21:21":"163"},{"09:41:21":"398"},{"09:51:21":"379"},{"10:11:15":"77"},{"11:01:15":"17"},{"11:21:34":"33"},{"12:02:59":"365"},{"12:12:59":"182"},{"12:22:00":"18"},{"12:32:00":"606"},{"13:50:17":"32"},{"14:01:24":"129"},{"14:52:57":"88"},{"18:12:50":"44"},{"18:22:50":"231"},{"18:42:50":"367"},{"20:02:19":"44"},{"20:42:19":"585"},{"21:12:19":"221"},{"21:32:19":"241"},{"21:42:19":"213"}]',
//		"hrSportData": '[{"09:01:22":"98"},{"09:11:22":"103"},{"09:21:22":"101"},{"09:31:22":"87"},{"09:51:23":"107"},{"10:12:16":"89"},{"10:22:16":"87"},{"10:32:16":"91"},{"10:42:16":"85"},{"10:52:16":"80"},{"11:22:36":"71"},{"11:32:36":"75"},{"11:42:33":"81"},{"12:00:01":"105"},{"12:10:01":"89"},{"12:20:01":"86"},{"12:30:01":"102"},{"12:40:01":"96"},{"12:50:01":"74"},{"13:00:01":"77"},{"13:10:01":"72"},{"13:20:01":"74"},{"13:30:02":"73"},{"13:40:02":"68"},{"14:01:26":"97"},{"15:16:58":"84"},{"15:26:58":"86"},{"15:36:58":"72"},{"15:48:58":"71"},{"15:58:26":"70"},{"16:29:10":"71"},{"17:14:52":"70"},{"17:24:52":"81"},{"17:34:52":"72"},{"17:44:52":"70"},{"18:14:51":"65"},{"18:24:51":"108"},{"18:44:51":"111"},{"18:54:51":"76"}]',
//		// "hrSportData": "",
//		"scoreDuration": "1.5",
//		"scoreTime": "2.5",
//		"scoreStep": "3.5",
//		"scoreStrength": "4.5",
//		"sportCode": "建议加大无氧运动量建议333333",
//		"totalNumber": "1000",
//		"validDuration": "2小时10分钟",
//		"validNumber": "8000",
//		"validHr": "100",
//		"updateTime": "14:31:21",
//		"addTime": 1521802900000,
//		"status": "unprocessed",
//		"arrayList": [{
//			"total_number": "9000",
//			"valid_number": "8000",
//			"data_date": "2018-03-23"
//		}]
//	}
//};
//
//fn_option('2018-04-03', dataTemp1.data);


function fn_option_sport(time, data) {

//	 $('.unEmpty').removeClass('dn');$('.empty').addClass('dn');

	//页面展示信息
	//评星
	star_sun('.sport_star1', data.scoreTime); //运动时间
	star_sun('.sport_star2', data.scoreStep); //运动步数
	star_sun('.sport_star3', data.scoreStrength); //运动强度

	$(".olState_yd li").html(data.sportCode); //运动状态提示

	//运动状态和心率统计
	if(data.totalNumber) {
		$(".sport0_1").html(data.totalNumber);
	} //总步数
	if(data.validDuration) {
		$(".sport0_2").html(data.validDuration.split('小时')[0]);
	} //有效运动时长
	if(data.validDuration) {
		$(".sport0_3").html(data.validDuration.split('小时')[1].split('分钟')[0]);
	} //有效运动时长
	if(data.validNumber) {
		$(".sport0_4").html(data.validNumber);
	} //有效运动步数

	//数据处理
	var last16 = dayList(time).last16; //日期
	var dataTemp = dayList(time).ecShow; //echart日期
	var uesTemp = dayList(time).ecUse; //echart日期下标

	//运动-数据
	var sport1_data1 = [],
		sport1_data2 = [];
	var data_arr1 = data.arrayList;
	if(data_arr1) {
		$.each(last16, function(key, val) { //日期数据比对，生成echart需要数据
			$.each(data_arr1, function(k, v) { //昼夜节律趋势分析
				if(val == v.data_date) {
					sport1_data1[key] = v.valid_number;
					sport1_data2[key] = parseInt(v.total_number) - parseInt(v.valid_number);
				}
			});
		});
		if(sport1_data1.length < 16) {
			sport1_data1[15] = '';
		} //数组不足16个时
		if(sport1_data2.length < 16) {
			sport1_data2[15] = '';
		}
	}
	//运动状态和心率统计
	var useHourTemp = ['0:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '3:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '6:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '9:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '12:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '15:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '18:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '21:00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '24:00'];

	if(data.sportData) {
		var sportData = JSON.parse(data.sportData); //步数
	}
	if(data.hrSportData) {
		var hrSportData = JSON.parse(data.hrSportData); //心率
	}

	// console.log(sportData);
	// console.log(hrSportData);

	var ten_minute = [];
	for(var m = 0; m < 145; m++) {
		ten_minute[m] = m * 10;
	}

	var sportDataTxt = []; //文字
	var hrSportDataTxt = []; //文字
	var sport_data1 = []; //步数
	var sport_data2 = []; //心率
	var sport_data2_txt = []; //心率文字

	if(sportData) {
		$.each(sportData, function(key, val) { //步数
			$.each(val, function(k, v) {
				var minutesTemp = Math.floor(k.split(':')[0] * 60) + Math.floor(k.split(':')[1]);
				$.each(ten_minute, function(m, n) {
					if(Math.abs(minutesTemp - n) <= 5) { //小于等于5
						sport_data1[m] = v;
						sportDataTxt[m] = k;
					}
				})
			})
		});
	}

	if(hrSportData) {
		$.each(hrSportData, function(key, val) { //心率
			$.each(val, function(k, v) {
				var minutesTemp = Math.floor(k.split(':')[0] * 60) + Math.floor(k.split(':')[1]);
				$.each(ten_minute, function(m, n) {
					if(Math.abs(minutesTemp - n) <= 5) { //小于等于5
						// sport_data2[m] = v*10;
						sport_data2[m] = v;
						sport_data2_txt[m] = v; //心率文字
						hrSportDataTxt[m] = k;
					}
				})
			})
		});
		//console.log(sport_data2_txt);
	}

	if(sport_data1.length < 145) {
		sport_data1[144] = '';
	} //数组不足145个时
	if(sportDataTxt.length < 145) {
		sportDataTxt[144] = '';
	}
	if(sport_data2.length < 145) {
		sport_data2[144] = '';
	}
	if(hrSportDataTxt.length < 145) {
		hrSportDataTxt[144] = '';
	}

	if(sport_data2_txt.length < 145) {
		sport_data2_txt[144] = '';
	}

	// console.log(sport_data2);
	// console.log(hrSportDataTxt);

	/*if(sport_data2){//补缺失1位的不足
	    $.each(sport_data2, function(key, val) {//心率
	        if(!val && sport_data2[key-1] && sport_data2[key+1]){
	            sport_data2[key] = (sport_data2[key-1]+sport_data2[key+1])/2
	        }
	    });
	}*/
	if(sport_data2) { //补缺失2位以及以下的不足
		$.each(sport_data2, function(key, val) { //心率
			if(!val && sport_data2[key - 1] && sport_data2[key + 1]) { //缺一位
				sport_data2[key] = (parseInt(sport_data2[key - 1]) + parseInt(sport_data2[key + 1])) / 2
			} else if(!val && sport_data2[key - 1] && sport_data2[key + 2]) { //缺两位
				sport_data2[key] = (parseInt(sport_data2[key - 1]) + parseInt(sport_data2[key + 2])) / 2;
				sport_data2[key + 1] = (parseInt(sport_data2[key - 1]) + parseInt(sport_data2[key + 2])) / 2;
			}
		});
	}

	// 运动0
	var option_yd1 = {
		grid: {
			y: 50,
			x2: 28,
			y2: 30,
			x: 46
		},
		tooltip: { // 提示框
			trigger: 'axis',
			showDelay: 0,
			hideDelay: 0,
			transitionDuration: 0,
			backgroundColor: '#fff',
			borderColor: '#999',
			borderWidth: 1,
			borderRadius: 4,
			textStyle: {
				color: '#666'
			},
			axisPointer: {
				type: 'line',
				lineStyle: {
					color: '#fd7a51',
					width: 2,
					type: 'solid'
				},
			},
			position: function(p) {
				if(p[0] > 200) {
					return [p[0] - 95, 28];
				} else {
					return [p[0], 28];
				}
			},
			formatter: function(params) { //自定义内容
				var timeTemp0 = sportDataTxt[params[0].dataIndex];
				var timeTemp1 = hrSportDataTxt[params[0].dataIndex];
				var timeTemp_rate = sport_data2_txt[params[0].dataIndex];

				var timeTemp = '';
				if(timeTemp0) {
					timeTemp = timeTemp0;
				} else if(timeTemp1) {
					timeTemp = timeTemp1;
				} else {
					timeTemp = '--:--:--';
				}
				var res = '<div class="tooltip"><p>' + timeTemp + '</p>';
				for(var i = 0; i < params.length; i++) {
					if(i == 0) {
						if(params[i].data) {
							res += '<p class="p_86">步数：' + params[i].data + '</p>'
						} else {
							res += '<p class="p_86">步数：---</p>'
						}
					} else if(i == 1) {
						if(timeTemp_rate) {
							res += '<p class="p_fd">心率：' + timeTemp_rate + '</p>'
						} else {
							res += '<p class="p_fd">心率：---</p>'
						}
					}
				}
				res += '</div>';
				return res;
			},
		},

		xAxis: {
			type: 'category',
			boundaryGap: false,
			data: useHourTemp,
			axisLabel: {
				interval: 17,
				textStyle: {
					color: '#999',
					fontSize: '8'
				}
			},
			axisLine: {
				lineStyle: {
					color: 'e7e5e4',
				}
			},
			axisTick: {
				show: false
			},
			splitArea: {
				show: false,
			}
		},
		yAxis: [{
			scale: true,
			max: 2000,
			min: 0,
			splitNumber: 5,
			type: 'value',
			axisLabel: {
				show: true,
				textStyle: {
					color: '#999'
				}
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted'
				}
			},
			axisLine: {
				show: false
			},
			axisTick: {
				show: false
			},
		}, {
			scale: true,
			max: 160,
			min: 0,
			interval: 40,
			type: 'value',
			axisLabel: {
				show: true,
				textStyle: {
					color: '#999'
				}
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted'
				}
			},
			axisLine: {
				show: false
			},
			axisTick: {
				show: false
			},
		}],
		color: ['#fd7b52', '#86d9e0'],
		legend: {
			selectedMode: false,
			x: 'left',
			padding: [0, 0, 0, 35],
			itemWidth: 15,
			itemHeight: 6,
			itemGap: 27,
			data: [{
				name: '心率',
				icon: 'rect',
			}, {
				name: '步数',
				icon: 'rect'
			}],
		},
		series: [{
			name: '步数',
			type: 'bar',
			barWidth: 1,
			data: sport_data1,
			itemStyle: {
				normal: {
					color: '#86d9e0'
				}
			},
		}, {
			yAxisIndex: 1,
			name: '心率',
			type: 'line',
			smooth: true,
			data: sport_data2,
			symbol: 'null',
			symbolSize: 0,
			itemStyle: {
				normal: {
					lineStyle: {
						color: '#fd7b52',
						width: 2
					}
				}
			},
		}],
	};

	var option_yd2 = {
		grid: {
			y: 50,
			x2: 14,
			y2: 30,
			x: 50
		},
		tooltip: { // 提示框
			trigger: 'axis',
			showDelay: 0,
			hideDelay: 0,
			transitionDuration: 0,
			backgroundColor: '#fff',
			borderColor: '#999',
			borderWidth: 1,
			borderRadius: 4,
			textStyle: {
				color: '#666'
			},
			axisPointer: {
				type: 'line',
				lineStyle: {
					color: '#fd7a51',
					width: 2,
					type: 'solid'
				},
			},
			position: function(p) {
				if(p[0] > 200) {
					return [p[0] - 100, 28];
				} else {
					return [p[0], 28];
				}
			},
			formatter: function(params) { //自定义内容
				var timeTemp = dataTemp[params[0].dataIndex].split('/');
				var timeShow = timeTemp[0] + '月' + timeTemp[1] + '日';
				var res = '<div class="tooltip"><p>' + timeShow + '</p>'
				for(var i = 0; i < params.length; i++) {
					var stepTemp = Math.floor(params[0].data) + Math.floor(params[1].data);
					if(i == 0) {
						if(params[i].data) {
							res += '<p class="p_00">有效步数 ' + params[i].data + '</p>'
						} else {
							res += '<p class="p_00">有效步数 ---</p>'
						}
					} else if(i == 1) {
						if(params[i].data) {
							res += '<p class="p_86">总步数 ' + stepTemp + '</p>'
						} else {
							res += '<p class="p_86">总步数 ---</p>'
						}
					}
				}
				res += '</div>';
				return res;
			},
		},
		xAxis: {
			type: 'category',
			data: uesTemp,
			axisLabel: {
				interval: 0,
				textStyle: {
					color: '#999'
				}
			},
			axisLine: {
				lineStyle: {
					color: 'e7e5e4',
				}
			},
			axisTick: {
				show: false
			},
			splitArea: {
				show: false,
			}
		},
		yAxis: {
			scale: true,
			min: 0,
			splitNumber: 5,
			type: 'value',
			axisLabel: {
				show: true,
				textStyle: {
					color: '#999'
				}
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted'
				}
			},
			axisLine: {
				show: false
			},
			axisTick: {
				show: false
			},
		},
		color: ['#86d9e0', '#0091ea', ],
		legend: { //图例
			selectedMode: false,
			x: 'left',
			padding: [0, 0, 0, 35],
			itemWidth: 15,
			itemHeight: 6,
			itemGap: 27,
			data: [{
				name: '总步数',
				icon: 'rect',
			}, {
				name: '有效步数',
				icon: 'rect'
			}],
		},
		series: [{ //数据
			name: '有效步数',
			type: 'bar',
			stack: '步数',
			barWidth: 6,
			data: sport1_data1,
			itemStyle: {
				normal: {
					color: '#0091ea'
				}
			},
		}, {
			name: '总步数',
			type: 'bar',
			stack: '步数',
			barWidth: 6,
			data: sport1_data2,
			itemStyle: {
				normal: {
					color: '#86d9e0'
				}
			},
		}],
	};
	echarts.init(document.getElementById("sport0")).setOption(option_yd1, true);
	echarts.init(document.getElementById("sport1")).setOption(option_yd2, true);

}